<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="姓名"
                    prop="name">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号"
                    prop="mobile">
        <el-input v-model="dataForm.mobile"></el-input>
      </el-form-item>
      <el-form-item label="微信号"
                    prop="wechat">
        <el-input v-model="dataForm.wechat"></el-input>
      </el-form-item>
      <el-form-item label="头像"
                    prop="headImage">
        <pic-upload v-model="dataForm.headImage" />
        <div>1-10张图片、格式：图片格式:JPG/JPEG/PNG</div>
      </el-form-item>
      <el-form-item label="微信二维码"
                    prop="wechatQrcode">
        <pic-upload v-model="dataForm.wechatQrcode" />
        <div>1-10张图片、格式：图片格式:JPG/JPEG/PNG, 最大5M、最佳尺寸建议1200*800</div>
      </el-form-item>
      <el-form-item label="企业名称">
        <el-select v-model="dataForm.factoryIds" multiple placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in factoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  // import PicUpload from '@/components/pic-upload'
  import PicUpload from '@/components/pic-upload/cmp_pic_upload'
  export default {
    data () {
      return {
        visible: false,
        roleList: [],
        factoryList:[],
        dataForm: {
          id: '',
          name: '',
          mobile: '',
          wechat: '',
          headImage:'',
          wechatQrcode: '',
          factoryIds:[]
        },
        dataRule: {
          name: [{
            required: true, message: '名称不能为空', trigger: 'blur'
          }]
        }
      }
    },
    components: {
      PicUpload
    },
    methods: {
      init (row) {
        console.log(row)
        console.log(this.dataForm)
        this.getFactoryList()
        this.dataForm.id = row ? row.id : 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.getJobContactDetail(this.dataForm.id)
            // this.dataForm.name = row.name
            // this.dataForm.mobile = row.mobile
            // this.dataForm.wechat = row.wechat
            // this.dataForm.headImage= row.headImage
            // this.dataForm.wechatQrcode = row.wechatQrcode
          }
          if(this.dataForm.id === 0){
            this.dataForm.factoryIds = []
          }
        })
      },
      getJobContactDetail(id){
        this.$http({
          url: this.$http.adornUrl('/v1/job/contact/detail'),
          method: 'get',
          params: this.$http.adornParams({id})
        }).then(({ data }) => {
          this.dataForm = data.data
        })
      },
      getFactoryList () {
        this.$http({
          url: this.$http.adornUrl('/v1/factory/all/list'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({ data }) => {
          this.factoryList = data.data
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(this.dataForm.id ? '/v1/job/contact/update' : '/v1/job/contact/add'),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        })
      }
    }
  }
</script>
